<template>
	<view class="slipPass">
		<view class="slip_main">
			<view class="inp">
				<input class="name my-pdl-15" placeholder-class="my-cl-grey my-font-14" type="text" placeholder="请输入手机号"
					v-model.lazy="phone" />
				<view class="inp_idcode_box  my-mgt-15">
					<input class="code my-pdl-15" placeholder-class="my-cl-grey my-font-14" type="text"
						placeholder="请输入验证码" v-model="idCode" />
					<template>
						<view class="codeBtn" v-if="anew">{{countDownNum}}s后重新获取</view>
						<view class="codeBtn" v-else @click="getCode">获取验证码</view>
					</template>
				</view>
				<view class="neweye">
					<input class="pass my-mgt-15 my-pdl-15" @blur="passrule('news')" placeholder-class="my-cl-grey my-font-14"
						type="text" placeholder="新密码(6~16位大小写字母、数字)" v-model="newPass" />
					<input class="name my-mgt-15 my-pdl-15" placeholder-class="my-cl-grey my-font-14" @blur="passrule('comir')" type="password"
						placeholder="确认密码(6~16位大小写字母、数字)" v-model="surePass" />
					
				</view>
			</view>
			<view class="slip-btn">
				<button class="btn" type="default" @click="confirm">确认</button>
			</view>
		</view>
		<u-popup v-model="QSPopup" mode="center">
			<view class="codebox">
				<view class="tlt my-mgb-10">请输入图形验证码</view>
				<view class="image-code">
					<input type="text" v-model="verifyCode" class="input" placeholder="输入图片中的图形" />
					<view class="img" @click="getVCode">
						<image :src="imageCode" mode="" class="code-img"></image>
						<view class="tishi">看不清？换一张</view>
					</view>
				</view>
				<view class="btnbox">
					<view class="left" @click="cancel">取消</view>
					<view class="right" @click="sendCode">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getVerifyCodeApi,
		sendMsgApi,
		forgotPasswordApi,
		authorizedlogApi
	} from "@/api/uesr.js"
	export default {
		data() {
			return {
				phone: null,
				idCode: null,
				newPass: null,
				surePass: null,
				seen: false,
				countDownNum: 60, // 倒计时初始值
				anew: false, //重新获取
				imageCode: '', //图形验证码
				QSPopup: false, //图形验证控制
				verifyCode: '',
				imgIcon: 'https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/snVKZMXaHU_close_eye.png',
			};
		},
		methods: {
			passrule(name) {
				if(name==="news"&&(this.newPass.length<6||this.newPass.length>16)){
					uni.showToast({
						mask:true,
						title:"请输入6-16位大小写字母、数字",
						icon:"none"
					})
					this.newPass=null
				}else if(name ==="comir"&&(this.surePass.length<6||this.surePass.length>16)){
					uni.showToast({
						mask:true,
						title:"请输入6-16位大小写字母、数字",
						icon:"none"
					})
					this.surePass = null
				}
				
			},
			async confirm() {
				//debugger;
				if(this.phone===null){
					uni.showToast({
						mask: true,
						title: "请输入手机号！",
						icon: "none"
					})
					return
				}else if (this.idCode === '') {
					uni.showToast({
						mask: true,
						title: "请输入验证码！",
						icon: "none"
					})
					return
				} else if (this.newPass === null) {
					uni.showToast({
						mask: true,
						title: "请输入新密码！",
						icon: "none"
					})
					return
				} else if (this.surePass === null) {
					uni.showToast({
						mask: true,
						title: "请输入确认密码！",
						icon: "none"
					})
					return
				} else {
					let params = {
						smsCode: this.idCode,
						newPassword: this.newPass,
						confirmPassword: this.surePass,
						oldPassword: null,
						phone:this.phone,
					}
					uni.showLoading({
						mask: true,
						title: "正在加载中...."
					})
					const res = await forgotPasswordApi(params).catch((e) => {
						if (e.statusCode === 400) {
							uni.hideLoading()
							this.idCode = null
							this.newPass = null
							this.surePass = null
						}
					})
					if (res.statusCode === 200) {
						uni.hideLoading()
						uni.showToast({
							mask: true,
							title: "修改成功",
							icon: "success",
							success() {
								setTimeout(() => {
									uni.redirectTo({
										url: "/pages/login/login"
									})
								}, 1500)
							}
						})
					}
				}
			},
			loogpass() {
				let openIcon = 'https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/OSUdUkrYKu_open_eye.png'
				let closeIcon = 'https://hxc-pay.oss-cn-shenzhen.aliyuncs.com/snVKZMXaHU_close_eye.png'
				this.seen ? (this.imgIcon = closeIcon) : (this.imgIcon = openIcon)
				this.seen = !this.seen
			},
			// 验证码倒计时
			getCode() {
				if (this.phone) {
					var regex = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
					var bool = regex.test(this.phone)
					if (bool) {
						this.QSPopup = true
						this.getVCode()
					} else {
						uni.showToast({
							title: "请输入正确的手机号",
							icon: "none"
						})
					}
				} else {
					uni.showToast({
						title: "请输入手机号",
						icon: "none"
					})
				}
			},
			countDown() {
				var _this = this;
				_this.anew = true
				var countDownNum = _this.countDownNum // 获取倒计时初始值
				var timer = setInterval(() => {
					countDownNum -= 1
					_this.countDownNum = countDownNum
					if (countDownNum <= -1) {
						clearInterval(timer)
						// 取消置顶的setInterval函数将要执行的代码
						_this.countDownNum = 60
						_this.anew = false

					}
				}, 1000)
			},
			// 获取图形验证码
			async getVCode() {
				uni.showLoading({
					title: "正在加载中...."
				})
				const res = await getVerifyCodeApi({})
				if (res.statusCode === 200) {
					uni.hideLoading()
					let imageBase64 = 'data:image/png;base64,' + wx.arrayBufferToBase64(res.data)
					this.imageCode = imageBase64
				} else {
					uni.showToast({
						title: '出错了，请重新获取',
						icon: "none"
					})
				}
			},
			//取消
			cancel() {
				this.QSPopup = false;
			},
			// 发送验证码
			async sendCode(ref) {
				if (this.verifyCode === "") {
					uni.showToast({
						title: "请先输入图形验证码！",
						icon: "none"
					})
					return
				} else {
					let params = {
						phone: this.phone,
						verifyCode: this.verifyCode
					}
					uni.showLoading({
						title: '正在加载中....'
					})
					const res = await sendMsgApi(params)
					console.log(res, "res");
					if (res.statusCode == 200) {
						uni.hideLoading()
						this.QSPopup = false;
						this.anew = true
						this.countDown()
						this.authorizedLog()
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				}
			},
			authorizedLog(){
				wx.login({
					success:(res)=>{
						var code = res.code;
						this.authlogin(code)
					}
				})
			},
			async authlogin(code){
				let _this = this
				let params = {
					jsCode:code
				}
				uni.showLoading({
					title:"正在加载中...."
				})
				const res = await authorizedlogApi(params)
				if(res.statusCode === 200){
					uni.hideLoading()
					uni.setStorageSync('openId', res.data.data.openId)
					uni.setStorageSync('sessionKey', res.data.data.sessionKey)
				}
			},
			// 返回上一页
			previous() {
				uni.redirectTo({
					url: "/pages/login/login"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.slipPass {
		width: 100%;
		height: 100vh;
		background: #FAFAFA;

		.slip_main {
			width: 100%;
			height: 800rpx;
			background: #FAFAFA;

			.inp {
				width: 100%;
				height: 600rpx;
				padding: 47rpx 30rpx 0rpx 30rpx;
				box-sizing: border-box;

				.name,
				.pass {
					width: 95%;
					height: 80rpx;
					background: #EBEBEB;
					box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.08);
					border-radius: 10rpx;
				}

				.neweye {
					position: relative;
				}

				.icon {
					width: 60rpx;
					height: 60rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					top: 10rpx;
					right: 35rpx;
					z-index: 99;

					image {
						width: 34rpx;
						height: 34rpx;
					}
				}

				.inp_idcode_box {
					width: 100%;
					height: 80rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					.code,
					.codeBtn {
						height: 80rpx;
						box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.08);
					}

					.code {
						width: 58%;
						background: #EBEBEB;
						border-radius: 10rpx 0rpx 0rpx 10rpx;
						margin-right: 30rpx;
					}

					.codeBtn {
						width: 32%;
						background: #F05329;
						border-radius: 6rpx;
						text-align: center;
						line-height: 80rpx;
						font-size: 26rpx;
						font-weight: 500;
						color: #FFFFFF;
					}
				}
			}

			.slip-btn {
				width: 100%;
				height: 88rpx;
				margin: auto;

				.btn {
					width: 90%;
					height: 100%;
					text-align: center;
					line-height: 88rpx;
					background: #F05329;
					border-radius: 10rpx;
					font-size: 38rpx;
					color: #FFFFFF;
				}
			}
		}

		.codebox {
			width: 690rpx;
			height: 376rpx;
			margin: auto;
			background: #FFFFFF;
			border-radius: 20rpx;

			.tlt {
				width: 100%;
				height: 110rpx;
				text-align: center;
				line-height: 110rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.image-code {
				width: 100%;
				height: 130rpx;
				display: flex;
				padding-left: 30rpx;

				.input {
					font-size: 32rpx;
					color: #000;
					width: 350rpx;
					height: 70rpx;
					border: 1rpx solid #999999;
					border-radius: 10rpx;
					margin-right: 30rpx;
					padding-left: 20rpx;
				}

				.img {
					width: 270rpx;
					height: 100%;

					.code-img {
						width: 90%;
						height: 80upx;
					}

					.tishi {
						width: 90%;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}

			.btnbox {
				width: 100%;
				height: 95rpx;
				margin-top: 23rpx;
				display: flex;
				border-top: 1rpx solid #999999;

				.right,
				.left {
					width: 50%;
					height: 100%;
					text-align: center;
					line-height: 95rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
				}

				.right {
					color: #F05329;
					border-left: 1rpx solid #999999;
				}

				.left {
					color: #999999;
				}
			}
		}
	}
</style>
